package com.javacoo.sftp.client.internal;

import java.util.Map;

import org.apache.commons.pool2.impl.GenericKeyedObjectPool;

import com.javacoo.sftp.client.config.SftpClientConfig;
import com.javacoo.sftp.client.config.SftpPoolConfig;
import com.javacoo.sftp.client.exception.SftpClientException;
import com.javacoo.sftp.client.template.SftpTemplate;

import lombok.extern.slf4j.Slf4j;

/**
 * SftpTemplate连接池
 * <li></li>
 * @author duanyong@javacoo.com
 * @date 2020/5/14 12:37
 */
@Slf4j
public class SftpTemplatePool{
    /** SftpTemplate 对象池 */
    private GenericKeyedObjectPool<SftpClientConfig,SftpTemplate> pool;
    /** sftp服务器配置 */
    private Map<String, SftpClientConfig> clients;
    /** sftp连接池配置 */
    private SftpPoolConfig sftpPoolConfig;

    public SftpTemplatePool(Map<String, SftpClientConfig> clients,SftpPoolConfig sftpPoolConfig) {
        this.clients = clients;
        this.sftpPoolConfig = sftpPoolConfig;
        this.pool = new GenericKeyedObjectPool<>(new SftpTemplateFactory());
        setConfig(sftpPoolConfig);
    }
    /**
     * 设置config
     * @author duanyong@javacoo.com
     * @date 2020/9/27 16:32
     * @param conf:
     * @return: void
     */
    private void setConfig(SftpPoolConfig conf) {
        this.pool.setLifo(conf.isLifo());
        this.pool.setMaxWaitMillis(conf.getMaxWaitMillis());
        this.pool.setBlockWhenExhausted(conf.isBlockWhenExhausted());
        this.pool.setTestOnCreate(conf.isTestOnCreate());
        this.pool.setTestOnBorrow(conf.isTestOnBorrow());
        this.pool.setTestOnReturn(conf.isTestOnReturn());
        this.pool.setTestWhileIdle(conf.isTestWhileIdle());
        this.pool.setNumTestsPerEvictionRun(conf.getNumTestsPerEvictionRun());
        this.pool.setMinEvictableIdleTimeMillis(conf.getMinEvictableIdleTimeMillis());
        this.pool.setTimeBetweenEvictionRunsMillis(conf.getTimeBetweenEvictionRunsMillis());
        this.pool.setSoftMinEvictableIdleTimeMillis(conf.getSoftMinEvictableIdleTimeMillis());
        final EvictionPolicy<SftpTemplate> policy = conf.getEvictionPolicy();
        if (policy == null) {
            // Use the class name (pre-2.6.0 compatible)
            this.pool.setEvictionPolicyClassName(conf.getEvictionPolicyClassName());
        } else {
            // Otherwise, use the class (2.6.0 feature)
            this.pool.setEvictionPolicy(policy);
        }
        this.pool.setEvictorShutdownTimeoutMillis(conf.getEvictorShutdownTimeoutMillis());
    }
    /**
     * 获取SftpTemplate
     * <li></li>
     * @author duanyong@javacoo.com
     * @date 2020/5/14 13:28
     * @param key: 客户端配置KEY
     * @return: com.javacoo.sftp.client.template.SftpTemplate
     */
    public SftpTemplate getClient(String key) throws Exception {
        if(!clients.containsKey(key)){
            log.error("客户端:{},未配置sftp连接信息",key);
            throw new SftpClientException("客户端:{},未配置sftp连接信息",key);
        }
        log.debug("SftpTemplatePool->getClient->{}",key);
        return pool.borrowObject(clients.get(key));
    }
    /**
     * 释放SftpTemplate
     * <li></li>
     * @author duanyong@javacoo.com
     * @date 2020/5/14 13:28
     * @param key: 客户端配置KEY
     * @param sftpTemplate:
     * @return: void
     */
    public void releaseClient(String key,SftpTemplate sftpTemplate) {
        try {
            pool.returnObject(clients.get(key),sftpTemplate);
            log.debug("SftpTemplatePool->releaseClient->{}",key);
        } catch (Exception e) {
            log.error("释放SftpTemplate异常:",e);
        }
    }
}
